<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        function ListNode(val, next) {
            this.val = (val === undefined ? 0 : val)
            this.next = (next === undefined ? null : next)
        }
        // https://leetcode-cn.com/problems/merge-two-sorted-lists/
        // 因为 null === 4 会直接返回错误，所以建议先判断空，再比较
        var mergeTwoLists = function (list1, list2) {
            let res = new ListNode(0, null);
            let p = res;
            while (list1 != null || list2 != null) {
                if (list1 === null && list2 != null) {
                    p.next = list2;
                    list2 = list2.next;
                } else if (list1 != null && list2 === null) {
                    p.next = list1;
                    list1 = list1.next;
                } else if (list1.val <= list2.val) {
                    p.next = list1;
                    list1 = list1.next;
                } else {
                    p.next = list2;
                    list2 = list2.next;
                } 
                p = p.next;
            }
            return res.next;
        };
        let node1 = new ListNode(1, null);
        let node2 = new ListNode(2, null);
        let node3 = new ListNode(4, null);

        let node4 = new ListNode(1, null);
        let node5 = new ListNode(3, null);
        let node6 = new ListNode(4, null);
        node1.next = node2;
        node2.next = node3;

        node4.next = node5;
        node5.next = node6
        mergeTwoLists(node1, node4);
    </script>
</body>

</html>